*WIC co-op eWIC new diff in diff analysis
*last modified: 3 December 2024
*last modified by: Charlotte Ambrozek
*this do file implements the csdid2 package to estimate individual 2x2 doubly robust did estimates and aggregate to obtain an att 

clear all
set more off
set rmsg on

set maxvar 120000
set emptycells drop

local data_dir ./data/cleaned
local raw_dir ./data/raw
local out_dir ./analysis/output
local graph_dir ./analysis/output/graphs
local tab_dir ./analysis/output/tables
local log_dir ./documentation/logs
local date: display %tdYY-NN-DD date(c(current_date), "DMY")
di "`date'"
capture log close

log using `log_dir'/ewic_csdid_tip_chainstatetime_`date', replace

*import cleaned eWIC implementation info into own frame
*eWIC implementation variable is at county fips/fiscal year level 
*partition data into state/treatment year groups

*import cleaned ewic implementation info into own frame
mkf ewic_fy
frame ewic_fy{
	use ./data/cleaned/ewic_rollout.dta, clear
	rename year fiscalyear
	drop pre* post* 
}

mkf income
frame income{
	use ./data/cleaned/countyyearmedianincome.dta, clear
	keep if year == 2005
	drop year
	destring ct_fips, replace
}

mkf countychars
frame countychars{
	use ./data/cleaned/countystats.dta, clear
	keep if year == 2005
	drop year
	destring ct_fips, replace
}

mkf demos
frame demos{
	use ./data/cleaned/demos.dta, clear
	keep if year == 2005
	drop year
	destring ct_fips, replace
}

*import TIP store data into own frame
mkf tip_fy
frame tip_fy{
	local data_dir ./data/cleaned
	use `data_dir'/tip_auth_sq, clear
	*drop "Direct Distribution Center" and "Home Food Delivery Contractor" types
	gen f = (vendor_type1 == 3 & (vendor_type2 == 4 | missing(vendor_type2))) | (vendor_type1 == 4 & (vendor_type2 == 3 | missing(vendor_type2)))
	bys tip_id: egen flag = mode(f)
	bys tip_id: replace vendor_type1 = vendor_type1[_n+1] if flag == 0 & f == 1
	drop if flag == 1
	drop flag f tip_year_id
	*fill in gaps in state fips using modes
	generate str_fips = string(ct_fips)
	replace str_fips = "0" + str_fips if strlen(str_fips) == 4
	generate st_fips = substr(str_fips, 1, 2)
	destring st_fips, replace 
	
	*drop Vermont and mississippi because of different regiemes prior to eWIC
	*drop NV because ewic turn on then off then on again and we don't have reliable timings
	*drop missing st_fips
	drop if st_fips == 50 | st_fips == 32 | st_fips == 28 | missing(st_fips)

	*flag stores that specialize in WIC
	gen a50 = vendor_type1 == 1 | vendor_type1 == 7 | vendor_type2 == 1 | vendor_type2 == 7
	
	*link in low income/high poverty info
	frlink m:1 ct_fips, frame(income) generate(inc_link)
	frget lowinc highpov highwicelig, from(inc_link)

	*link ewic implementation info
	frlink m:1 ct_fips fiscalyear, frame(ewic_fy) generate(ewic_link)
	frget ev_year , from(ewic_link)
	* assume treated at earliest exposure
	bys tip_id: egen ey_min = min(ev_year)
	
	* assume treated at earliest exposure
	replace ev_year = ey_min if ev_year != ey_min & !missing(ey_min)
	bys tip_id (ev_year): assert ev_year[1] == ev_year[_N]

	*assert no dups at future level of xtset 
	duplicates report tip_id fiscalyear 
	assert r(N) == r(unique_value)
	xtset tip_id fiscalyear
	compress
}

cwf tip_fy
* for chains, construct two alternative treatment timings:
* 1) based on the first implementation of eWIC in the state for each chain
* 2) based on the first implementation of eWIC by processor for each chain
frame put ev_year vendor_name state st_fips if chain == 1, into(chain_timings)

cwf chain_timings
duplicates drop

* clean up chain names because these will be needed to define the timings
gen name = vendor_name
*first, make seven eleven text
replace name = regexr(name, "^7\s11", "SEVEN ELEVEN")
* find position of first numeric character after at least one alpha character, and drop rest of string
* this should drop all store numbers, etc
*first, find position of first alpha character
moss name, match("([A-Za-z])") regex max(1)
rename _pos1 alphapos
drop _count _match1
moss name, match("([0-9])") regex
drop _match*
gen droppos = .
forval i = 9(-1)1{
	replace droppos = _pos`i' if _pos`i' > alphapos & _count > 1 & _pos`i' >= 4
}
replace name = substr(name, 1, droppos - 1)
replace name = strtrim(name)
replace name = regexr(name, "[0-9]$", "") if !missing(_pos1) & _pos1 > 2
replace name = strtrim(name)
drop _count _pos* alphapos droppos

bys name st_fips: egen state_min_ev_year = min(ev_year)

* it looks like there are a couple of stores in that won't be entirely covered by the above procedure:
*	need to identify all regex matches to stores in the state with that regex and then take the min for that regex
replace state_min_ev_year = 2018 if state == "NY" & regexm(name, "BYRNE DAIRY")
replace state_min_ev_year = 2008 if state == "TX" & regexm(name, "GROCERY SVC")
replace state_min_ev_year = 2009 if state == "KY" & regexm(name, "KROGER")
replace state_min_ev_year = 2005 if state == "TX" & regexm(name, "MINYARD")
replace state_min_ev_year = 2004 if state == "TX" & regexm(name, "TARGET")
replace state_min_ev_year = 2004 if state == "TX" & regexm(name, "WALMART")
replace state_min_ev_year = 2014 if state == "OH" & regexm(name, "SAVE A LOT")
replace state_min_ev_year = 2006 if state == "TX" & name == "SUPER S"
replace state_min_ev_year = 2013 if state == "FL" & regexm(name, "TARGET")
replace state_min_ev_year = 2022 if state == "GA" & regexm(name, "TARGET")
replace state_min_ev_year = 2018 if state == "NH" & regexm(name, "TARGET")
replace state_min_ev_year = 2021 if state == "NJ" & regexm(name, "TARGET")
replace state_min_ev_year = 2018 if state == "NY" & regexm(name, "TARGET")
replace state_min_ev_year = 2019 if state == "PA" & regexm(name, "WEIS")

cwf tip_fy
frlink m:1 vendor_name ev_year state st_fips, frame(chain_timings) generate(chain_time_link)
frget state_min_ev_year, from(chain_time_link)

replace state_min_ev_year = ev_year if chain == 0

drop chain_time_link

cwf tip_fy
drop if missing(state_min_ev_year)

encode state, generate(state_code)

*link in controls
frlink m:1 ct_fips, frame(countychars) generate(countylink)
frget medincome sharepoor shareltwic sharelt2fpl sharesnap sharecash sharecashorsnap, from(countylink)
frlink m:1 ct_fips, frame(demos) generate(demoslink)
frget total_pop under5_pop hispanic_total_pop total_black_pop region division urbanrural share_black share_hispanic share_under5, from(demoslink)
gen log_pop = log(total_pop)
reg state_min_ev_year log_pop c.share_under5##c.shareltwic share_black share_hispanic sharesnap sharecash medincome i.region i.urbanrural
outreg2 using ./analysis/output/tables/chainstatetimingregression.tex, replace
reg state_min_ev_year log_pop c.share_under5##c.shareltwic share_black share_hispanic sharesnap sharecash medincome i.urbanrural i.state_code
outreg2 using ./analysis/output/tables/chainstatetimingregressionwinstate.tex, replace

frame drop ewic_fy

mkf csdidsimple float(b se pval ll ul) str25(outcome subsample state controls)
mkf csdidevent float(b se pval ll ul) int(ev_yr) str25(outcome subsample state controls)
mkf csdideventavg float(avgb avgse avgpval avgll avgul) int(times) str25(time outcome subsample state controls)
mkf csdidgroup float(b se pval ll ul) str25(gr_yr outcome subsample state controls)

cwf tip_fy
cd `out_dir'
csdid auth , ivar(tip_id) time(fiscalyear) gvar(state_min_ev_year) notyet long2 saverif(csdid_tip_sttime_auth) replace cluster(st_fips)
mkf post
cwf post
use csdid_tip_sttime_auth, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("all") ("all") ("no")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("all") ("all") ("no")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("all") ("all") ("no")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("all") ("all") ("no")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("all") ("all") ("no")
}
* same estimation but with covariates for local (county level) economic conditions now
cwf tip_fy
csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(state_min_ev_year) notyet long2 saverif(csdid_tip_sttime_auth_ctrls) replace cluster(st_fips)
cwf post
use csdid_tip_sttime_auth_ctrls, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("all") ("all") ("yes")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("all") ("all") ("yes")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("all") ("all") ("yes")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("all") ("all") ("yes")
}

csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("all") ("all") ("yes")
}

*estimation for processor graphs
*need state specific estimates to link back in processor order
*make a temp frame that is state of interest plus all states treated only after data end;
* then estimates represent treatment effect in state
* don't save event study estimates here, because we'll just need the state average treatment effect for the plot
foreach state in "VA" "MI" "OK" "NM" "TX" "MA" "WV" "FL" "KY" "WI" "OR"{
	cwf tip_fy
	frame put _all if ((state == "`state'") | inlist(st_fips, 1, 2, 6, 11, 13, 15, 16, 17, 22, 23, 27, 28, 29, 34, 38, 42, 44, 45, 49, 53)), into(state)
	cwf state
	csdid auth, ivar(tip_id) time(fiscalyear) gvar(state_min_ev_year) notyet long2 saverif(csdid_tip_sttime_auth_`state') replace cluster(st_fips)
	cwf post
	use csdid_tip_sttime_auth_`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("all") ("`state'") ("no")
	cwf state
	csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(state_min_ev_year) notyet long2 saverif(csdid_tip_sttime_auth_ctrls_`state') replace cluster(st_fips)
	cwf post 
	use csdid_tip_sttime_auth_ctrls_`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("all") ("`state'") ("yes")
	frame drop state
}

cwf tip_fy
frame put _all if chain == 1, into(chain)
cwf chain

cd `out_dir'
csdid auth, ivar(tip_id) time(fiscalyear) gvar(state_min_ev_year) notyet long2 saverif(csdid_tip_sttime_authchain) replace cluster(st_fips)
cwf post
use csdid_tip_sttime_authchain, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("chain") ("all") ("no")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("chain") ("all") ("no")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("chain") ("all") ("no")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("chain") ("all") ("no")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("chain") ("all") ("no")
}
* chain only analysis but with controls this time
cwf chain
csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(state_min_ev_year) notyet long2 saverif(csdid_tip_sttime_authchain_ctrls) replace cluster(st_fips)
cwf post
use csdid_tip_sttime_authchain_ctrls, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("chain") ("all") ("yes")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("chain") ("all") ("yes")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("chain") ("all") ("yes")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("chain") ("all") ("yes")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("chain") ("all") ("yes")
}

*estimation for processor graphs
*need state specific estimates to link back in processor order
*make a temp frame that is state of interest plus all states treated only after data end;
* then estimates represent treatment effect in state
* don't save event study estimates here, because we'll just need the state average treatment effect for the plot
foreach state in "VA" "MI" "OK" "NM" "TX" "MA" "WV" "FL" "KY" "WI" "OR"{
	cwf chain
	frame put _all if ((state == "`state'") | inlist(st_fips, 1, 2, 6, 11, 13, 15, 16, 17, 22, 23, 27, 28, 29, 34, 38, 42, 44, 45, 49, 53)), into(state)
	cwf state
	csdid auth, ivar(tip_id) time(fiscalyear) gvar(state_min_ev_year) notyet long2 saverif(csdid_tip_sttime_authchain_`state') replace cluster(st_fips)
	cwf post
	use csdid_tip_sttime_authchain_`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("chain") ("`state'") ("no")
	cwf state
	csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(state_min_ev_year) notyet long2 saverif(csdidtipsttimeauthchainxs`state') replace cluster(st_fips)
	cwf post
	use csdidtipsttimeauthchainxs`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("chain") ("`state'") ("yes")
	frame drop state
}

cwf tip_fy
frame put _all if chain == 0, into(indep)
cwf indep
frame drop chain
* indepedent store estimation
cd `out_dir'
csdid auth, ivar(tip_id) time(fiscalyear) gvar(state_min_ev_year) notyet long2 saverif(csdid_tip_sttime_authindep) replace cluster(st_fips)
cwf post
use csdid_tip_sttime_authindep, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("indep") ("all") ("no")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("indep") ("all") ("no")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("indep") ("all") ("no")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("indep") ("all") ("no")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("indep") ("all") ("no")
}
*independent store estimation with covariates
cwf indep
csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(state_min_ev_year) notyet long2 saverif(csdid_tip_sttime_authindep_ctrls) replace cluster(st_fips)
cwf post
use csdid_tip_sttime_authindep_ctrls, clear
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("indep") ("all") ("yes")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("indep") ("all") ("yes")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("indep") ("all") ("yes")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("indep") ("all") ("yes")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("indep") ("all") ("yes")
}

*estimation for processor graphs
*need state specific estimates to link back in processor order
*make a temp frame that is state of interest plus all states treated only after data end;
* then estimates represent treatment effect in state
* don't save event study estimates here, because we'll just need the state average treatment effect for the plot
foreach state in "VA" "MI" "OK" "NM" "TX" "MA" "WV" "FL" "KY" "WI" "OR"{
	cwf indep
	frame put _all if ((state == "`state'") | inlist(st_fips, 1, 2, 6, 11, 13, 15, 16, 17, 22, 23, 27, 28, 29, 34, 38, 42, 44, 45, 49, 53)), into(state)
	cwf state
	csdid auth, ivar(tip_id) time(fiscalyear) gvar(state_min_ev_year) notyet long2 saverif(csdid_tip_sttime_authindep_`state') replace cluster(st_fips)
	cwf post
	use csdid_tip_sttime_authindep_`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("indep") ("`state'") ("no")
	cwf state
	csdid auth medincome hispanic_total_pop total_black_pop sharepoor shareltwic sharecashorsnap total_pop under5_pop, ivar(tip_id) time(fiscalyear) gvar(state_min_ev_year) notyet long2 saverif(csdidtipsttimeauthindepxs`state') replace cluster(st_fips)
	cwf post 
	use csdidtipsttimeauthindepxs`state', clear
	csdid_stats simple
	frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("indep") ("`state'") ("yes")
	frame drop state
}

/* cwf tip_fy
frame put _all if highwicelig == 1, into(highwicelig)
cwf highwicelig

cd `out_dir'
csdid auth, ivar(tip_id) time(fiscalyear) gvar(state_min_ev_year) notyet long2 saverif(csdid_tip_statetime_authhighwicelig) replace cluster(st_fips)
csdid_stats simple
frame post csdidsimple (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) ("auth") ("highwicelig") ("all") ("no")
csdid_stats event, window(-4 4)
*the structure of the returned matrix here is columns pre_avg post_avg tm4 tm3 ... tm1 tp0 tp1 ... tp4
*i assume that tm# is pre period # and tp# is post period #
* so we want to capture columns 3 through 11 for the event periods, plus the pre/post averages (in a separate frame that can be linked on later).
frame post csdideventavg (r(table)[1,1]) (r(table)[2,1]) (r(table)[4,1]) (r(table)[5,1]) (r(table)[6,1]) (4) ("pre") ("auth") ("highwicelig") ("all") ("no")
frame post csdideventavg (r(table)[1,2]) (r(table)[2,2]) (r(table)[4,2]) (r(table)[5,2]) (r(table)[6,2]) (5) ("post") ("auth") ("highwicelig") ("all") ("no")
local i = 2
forval y = -4/4{
	local i = `i' + 1
	frame post csdidevent (r(table)[1,`i']) (r(table)[2,`i']) (r(table)[4,`i']) (r(table)[5,`i']) (r(table)[6,`i']) (`y') ("auth") ("highwicelig") ("all") ("no")
}
csdid_stats group
local names: colnames r(table)
local names = subinstr("`names'", "G", "", .)
forval y = 1/10{
	local year: word `y' of `names'
	frame post csdidgroup (r(table)[1,`y']) (r(table)[2,`y']) (r(table)[4,`y']) (r(table)[5,`y']) (r(table)[6,`y']) ("`year'") ("auth") ("highwicelig") ("all") ("no")
} */

frame csdideventavg{
	expand times, generate(new)
	bys time outcome subsample controls (new): gen ev_yr = sum(new)
	replace ev_yr = ev_yr - 4 if time == "pre"
	drop time new times
}

cwf csdidevent
frlink 1:1 ev_yr outcome subsample controls, frame(csdideventavg)
frget avgb avgse avgpval avgll avgul, from(csdideventavg)
drop csdideventavg

frame drop csdideventavg

cwf csdidsimple
/* frame drop highwiceligindep */
save `out_dir'/csdid_tip_chainstatetime_simple.dta, replace
cwf csdidevent
save `out_dir'/csdid_tip_chainstatetime_event.dta, replace  
cwf csdidgroup
save `out_dir'/csdid_tip_chainstatetime_group.dta, replace  
log close 
